package com.wxapp.common.util.date;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Objects;

/**
 * @author lgl
 * @Description 日期工具类
 * @Datetime 2023/9/19 17:10
 * @Modified By
 */
public class DateTimeUtil {
    public static final  String YYYY_MM_DD ="yyyy-MM-dd";
    public static final  String YYYYMMDD ="yyyyMMdd";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String HHMMSS = "HHmmss";

    private DateTimeUtil() {
    }

    public static String format(final Date date, final String pattern) {
        if (date == null) {
            return null;
        }
        if (pattern == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String getTimeStamp(LocalDateTime time){
        return String.valueOf(Objects.requireNonNullElseGet(time, LocalDateTime::now)
                .toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
    }
    public static String getNowDate(final String pattern){
        return  format(new Date(),pattern);
    }
}
